##############################################################################
# Product: Makefile for QP/C for Windows and POSIX *HOSTS*
# Last updated for version 7.2.2
# Last updated on  2023-02-17
#
#                    Q u a n t u m  L e a P s
#                    ------------------------
#                    Modern Embedded Software
#
# Copyright (C) 2005 Quantum Leaps, LLC, <state-machine.com>.
#
# This program is open source software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Alternatively, this program may be distributed and modified under the
# terms of Quantum Leaps commercial licenses, which expressly supersede
# the GNU General Public License and are specifically designed for
# licensees interested in retaining the proprietary status of their code.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <www.gnu.org/licenses/>.
#
# Contact information:
# <www.state-machine.com/licensing>
# <info@state-machine.com>
##############################################################################
#
# examples of invoking this Makefile:
# building configurations: Debug (default), Release, and Spy
# make
# make CONF=rel
# make CONF=spy
# make clean   # cleanup the build
# make CONF=spy clean   # cleanup the build
#
# NOTE:
# To use this Makefile on Windows, you will need the GNU make utility, which
# is included in the QTools collection for Windows, see:
#    https://github.com/QuantumLeaps/qtools
#

#-----------------------------------------------------------------------------
# project name:
#
PROJECT := game-gui

#-----------------------------------------------------------------------------
# project directories:
#

# list of all source directories used by this project
VPATH := ./game-gui \

# list of all include directories needed by this project
INCLUDES := -I./game-gui \

# location of the QP/C framework (if not provided in an env. variable)
QPC := .

#-----------------------------------------------------------------------------
# project files:
#

# C source files...
C_SRCS := \
	bsp.c \
	game.c \
	main.c \
	mine1.c \
	mine2.c \
	missile.c \
	ship.c \
	tunnel.c

# resouce file...
RC_INCLUDE := -I./game-gui/Res
RC_SRCS := \
	game-gui.rc

# defines...
DEFINES   := -DQHSM_INIT=QHsm_init -DQHSM_DISPATCH=QHsm_dispatch

ifeq (,$(CONF))
	CONF := dbg
endif

#-----------------------------------------------------------------------------
# add QP/C framework:
#
C_SRCS += \
	qep.o \
	qfsm_ini.o \
	qfsm_dis.o \
	qhsm_ini.o \
	qhsm_dis.o \
	qhsm_top.o \
	qhsm_in.o \
	qa_defer.o \
	qa_fifo.o \
	qa_lifo.o \
	qa_get_.o \
	qa_sub.o \
	qa_usub.o \
	qa_usuba.o \
	qeq_init.o \
	qeq_fifo.o \
	qeq_lifo.o \
	qeq_get.o \
	qf_act.o \
	qf_gc.o \
	qf_log2.o \
	qf_new.o \
	qf_pool.o \
	qf_psini.o \
	qf_pspub.o \
	qf_pwr2.o \
	qf_tick.o \
	qmp_get.o \
	qmp_init.o \
	qmp_put.o \
	qte_ctor.o \
	qte_arm.o \
	qte_darm.o \
	qte_rarm.o \
	qte_ctr.o \
	qf_port.o \
	win32_gui.o

#============================================================================
# Typically you should not need to change anything below this line

VPATH    += $(QPC)/ports/win32/mingw $(QPC)/qep $(QPC)/qf
INCLUDES += -I$(QPC)/include -I$(QPC)/ports/win32/mingw -I$(QPC)/qep -I$(QPC)/qf

#-----------------------------------------------------------------------------
# GNU toolset:
#
# NOTE:
# GNU toolset (MinGW) is included in the QTools collection for Windows, see:
#     https://github.com/QuantumLeaps/qtools
# It is assumed that $(QTOOLS)/bin directory is added to the PATH
#
CC    := gcc
LINK  := gcc
RC    := windres

#-----------------------------------------------------------------------------
# basic utilities (depends on the OS this Makefile runs on):
#
ifeq ($(OS),Windows_NT)
	MKDIR      := mkdir
	RM         := rm
	TARGET_EXT := .exe
else ifeq ($(OSTYPE),cygwin)
	MKDIR      := mkdir -p
	RM         := rm -f
	TARGET_EXT := .exe
else
	MKDIR      := mkdir -p
	RM         := rm -f
	TARGET_EXT :=
endif

#-----------------------------------------------------------------------------
# build configurations...

ifeq (rel, $(CONF)) # Release configuration ..................................

BIN_DIR := build_rel
# gcc options:
CFLAGS  = -c -O3 -fno-pie -std=c11 -pedantic -Wall -Wextra -W \
	$(INCLUDES) $(DEFINES) -DNDEBUG

else # default Debug configuration .........................................

BIN_DIR := build
# gcc options:
CFLAGS  = -c -g -O -fno-pie -std=c11 -pedantic -Wall -Wextra -W \
	$(INCLUDES) $(DEFINES)

endif  # .....................................................................

LINKFLAGS := -mwindows
ifndef GCC_OLD
	LINKFLAGS += -no-pie
endif

#-----------------------------------------------------------------------------
C_OBJS       := $(patsubst %.c,%.o, $(C_SRCS))
RC_OBJS      := $(patsubst %.rc,%.o, $(RC_SRCS))
TARGET_EXE   := $(BIN_DIR)/$(PROJECT)$(TARGET_EXT)
C_OBJS_EXT   := $(addprefix $(BIN_DIR)/, $(C_OBJS))
C_DEPS_EXT   := $(patsubst %.o,%.d, $(C_OBJS_EXT))
RC_OBJS_EXT  := $(addprefix $(BIN_DIR)/, $(RC_OBJS))

# create $(BIN_DIR) if it does not exist
ifeq ("$(wildcard $(BIN_DIR))","")
$(shell $(MKDIR) $(BIN_DIR))
endif

#-----------------------------------------------------------------------------
# rules
#

.PHONY: clean show

all: $(TARGET_EXE)

$(TARGET_EXE) : $(C_OBJS_EXT) $(RC_OBJS_EXT)
	$(LINK) $(LINKFLAGS) -o $@ $^

$(BIN_DIR)/%.d : %.c
	$(CC) -MM -MT $(@:.d=.o) $(CFLAGS) $< > $@

$(BIN_DIR)/%.o : %.c
	$(CC) $(CFLAGS) $< -o $@

$(BIN_DIR)/%.o : %.rc
	$(RC) $(RC_INCLUDE) -i $< -o $@

# create BIN_DIR and include dependencies only if needed
ifneq ($(MAKECMDGOALS),clean)
  ifneq ($(MAKECMDGOALS),show)
     ifneq ($(MAKECMDGOALS),debug)
ifeq ("$(wildcard $(BIN_DIR))","")
$(shell $(MKDIR) $(BIN_DIR))
endif
-include $(C_DEPS_EXT) $(CPP_DEPS_EXT)
     endif
  endif
endif

clean :
	-$(RM) $(BIN_DIR)/*.o \
	$(BIN_DIR)/*.d \
	$(TARGET_EXE)

show :
	@echo PROJECT      = $(PROJECT)
	@echo TARGET_EXE   = $(TARGET_EXE)
	@echo VPATH        = $(VPATH)
	@echo C_SRCS       = $(C_SRCS)
	@echo C_DEPS_EXT   = $(C_DEPS_EXT)
	@echo C_OBJS_EXT   = $(C_OBJS_EXT)
	@echo C_DEPS_EXT   = $(C_DEPS_EXT)
	@echo DEFINES      = $(DEFINES)
